Java Magazine: Inside Java and the Jvm by Andrew Benstock

Java Magazine: Inside Java and the Jvm by Andrew Benstock

Author:Andrew Benstock [Benstock, Andrew]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2018-12-04T16:00:00+00:00


Each line in the snippet above is important. Let’s go over them one by one, starting from the bottom:

public @interface Test { The syntax here is speciic for annotations. Annotations look like Java classes but have several restrictions, which we will cover shortly.

@Target({METHOD})

This annotation speciies what Java elements can be anno- tated.METHOD is part of thejava.lang.annotation .ElementTypeenum, which lets you deine other locations for annotations such asCLASS,CONSTRUCTOR, and so on.

@Retention(RUNTIME) Attributes are methods that don’t have a body and that can optionally be assigned a default value. If you fail to use the defaultkeyword, then that attribute needs to be speciied when the annotation is used; otherwise, the compiler will issue an error. An important restriction on attributes is that they need to be constants: primitive types or a string (and they can’t be null).

There are two interesting details that were included in the speciication in order to reduce the amount of verbosity found in code using annotations.

If the annotation deines an attribute with the special name value, then you can specify that attribute without the word value. The following annotation:

public @interface Person { String value();

} This annotation indicates whether your annotation will be preserved in the class ile or discarded by the compiler. If you want to be able to look up your annotation via relec- tion, the retention should be set toRUNTIME. The other options can be found in thejava.lang.annotation .RetentionPolicy class.

can be written as

@Person("John")

instead of the more common

@Person(value = "John")

Content of an Annotation It is possible to pass additional parameters to annotations: In a similar vein, attributes of typeArray can use a shorthand version when that array has only one element:

@Test(description = "Verify that bug #121 is fixed") @Table(name = "ACCOUNTS")

These additional parameters are called attributes. They are deined as methods inside the declaration of your annotation:

public @interface Languages { String[] value();

}

can use37

@Languages("English") @Languages({ "English", "French"}) class MyClass { … }

instead of the more verbose

We can look up the annotation as follows: @Languages(value = { "English" })

These syntactic shortcuts were designed to reduce the boilerplate code necessary when using annotations.

Annotations in Action Now that you can deine simple annotations, how do you actually use them? By design, annotations deined by thirdparty developers are completely ignored by the compiler. There are a few speciic exceptions that the compiler acts upon (such as@Deprecated), but as a rule, annotations will never modify the semantics of the code to which they are applied. Therefore, the only way to make use of annotations is to write a tool that will act upon them.

There are two ways such tools can be written: as external tools or as annotation processors.

External Tools External tools are the simplest approach to processing exceptions: you implement a separate application with its own main() method, and users of your annotations simply need to run this tool on their classes. This is the approach used by TestNG, JUnit, Guice, and other well-known tools. Such tools are typically run as part of your build, and the output of these tools can be quite varied: source iles, documentation iles, XML, and so on.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.